<?php
/**
 * 广告管理
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         LinkController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
use Scshux\Utils\Tree;
use Scshux\Utils\Time;
use Scshux\Utils\Uploader;
class LinkController extends  ControllerBase
{

	/**
	 * 友情链接列表
	 */
	public function   indexAction()
	{
		$is_ajax  = $this->request->get('is_ajax','int',0);
		$dataList = $this->_getDataList();
		$this->view->setVar('dataList', $dataList);
		if($is_ajax){
			$this->view->setVar('full_page',0);
			$this->view->start();
			$this->view->render('link','index');
			$this->view->finish();
			$content = $this->view->getContent();
			$data = array(
					'content' => $content,
					'filter'  => $dataList->filter,
					'totalPage' => $dataList->totalPage,
					'recordCount'=>$dataList->totalSize,
					'currentPage'=>$dataList->currentPage,
					'extended'   =>$dataList->extended
			);
			$this->sendResult(true,'',$data);
		}else{
			$this->view->setVar('full_page',1);
		}

	}

	/**
	 * 二维码
	 */
	public function qrcodeAction()
	{
	    $is_ajax  = $this->request->get('is_ajax','int',0);
	    $dataList = $this->_getDataList(2);
	    $this->view->setVar('dataList', $dataList);
	    $this->view->setVar('totalnum',count($dataList->items));
	    if($is_ajax){
	        $this->view->setVar('full_page',0);
	        $this->view->start();
	        $this->view->render('link','qrcode');
	        $this->view->finish();
	        $content = $this->view->getContent();
	        $data = array(
	                'content' => $content,
	                'filter'  => $dataList->filter,
	                'totalPage' => $dataList->totalPage,
	                'recordCount'=>$dataList->totalSize,
	                'currentPage'=>$dataList->currentPage,
	                'extended'   =>$dataList->extended
	        );
	        $this->sendResult(true,'',$data);
	    }else{
	        $this->view->setVar('full_page',1);
	    }
	}
	
	
	/**
	 *  广告编辑
	 */
	public function   editAction()
	{
		$id = $this->request->get('id','int',0);
		if($this->request->isPost()){
			$adModel = new SModel('link');
			
			//加入语言
			$_POST['site_id'] = $this->getCore()->getSiteID();
			$postData = $_POST;

			if($_FILES['link_img']){
				$uploaderObj = new Uploader();
				$postData['link_img'] =	$uploaderObj->upload($_FILES['link_img']);
				if(empty($postData['link_img'])){
					unset($postData['link_img']);
				}
			}
			if($id>0){
				$adModel->setData($postData);
				$adModel->update('id='.$id);
			}else{
				unset($postData['id']);
				$adModel->setData($postData);
				$adModel->add();
			}
			return $this->redirect('link/index');
		}else{
			if($id>0){
				$adModel = new SModel('link');
				$item = $adModel->getObj('id='.$id);
				$this->view->setVar('item', $item);
			}
		}
	}
	
	
	/**
	 * 编辑or新增二维码
	 */
	public function editqrcodeAction()
	{
	    $id = $this->request->get('id','int',0);
	    if($this->request->isPost()){
	        $adModel = new SModel('link');
	        	
	        //加入语言
	        $_POST['site_id'] = $this->getCore()->getSiteID();
	        $postData = array(
	                'link_name'=>'',
	                'link_url'=>'',
	                'type'=>2,
	        );
	    
	        if($_FILES['link_img']){
	            $uploaderObj = new Uploader();
	            $postData['link_img'] =	$uploaderObj->upload($_FILES['link_img']);
	            if(empty($postData['link_img'])){
	                unset($postData['link_img']);
	            }
	        }
	        if($id>0){
	            $adModel->setData($postData);
	            $adModel->update('id='.$id);
	        }else{
	            unset($postData['id']);
	            $adModel->setData($postData);
	            $adModel->add();
	        }
	        return $this->redirect('link/qrcode');
	    }else{
	        if($id>0){
	            $adModel = new SModel('link');
	            $item = $adModel->getObj('id='.$id);
	            $this->view->setVar('item', $item);
	        }
	    }
	}
	

	/**
	 * 删除分类
	 */
	public  function  delAction()
	{
		$id = $this->request->get('id','int',0);
		if($id>0){
			$adModel = new SModel('link');
			$adModel->del("id='{$id}'");
		}
		return $this->redirect('link/index');
	}

	/**
	 * 删除二维码
	 */
	public function delqrcodeAction()
	{
	    $id = $this->request->get('id','int',0);
	    if($id>0){
	        $adModel = new SModel('link');
	        $adModel->del("id='{$id}'");
	    }
	    return $this->redirect('link/qrcode');
	}

	
	/**
	 * 列表数据
	 */
	protected   function  _getDataList($type=1)
	{
		$filter = array();
		$adModel = new SModel('link');
		
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		$where = ' type='.$type;
		
		$items = $adModel->query($where,'*','id','desc');

		$dataList = new \stdClass();
		$dataList->totalPage = 1;
		$dataList->currentPage = 1;
		$dataList->items = $items;
		$dataList->totalSize = count($items);
		$dataList->filter = $filter;
		return  $dataList;
	}

}